package org.testis.gui.mvc.view.testdesign;

import org.testis.gui.mvc.view.*;
import org.testis.gui.mvc.controller.testdesign.PopupController;
import org.testis.gui.mvc.controller.*;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.util.LinkedList;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButton;
import org.orm.PersistentException;
import org.testis.orm.refinedclasses.DesignPart;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestStepRefined;
import org.testis.utils.middleware.TestDesign;

/**
 *
 * @author Cahit Emir Erel
 */
public class PopupViewPanel extends JPopupMenu implements AbstractViewPanel {

    private static final long serialVersionUID = 5434522L;
    //  The controller used by this view
    private boolean processSelectedOnly;
    public PopupViewPanel() {
        super();
        processSelectedOnly = false;

        initComponents();
        localInitialization();
    }

    // <editor-fold defaultstate="collapsed" desc=" Local Initialization ">                          
    /**
     * Initialization method called from the constructor
     */
    public void localInitialization() {
    }

    // </editor-fold>
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPopupMenu1 = this;
        addTestPlan = new javax.swing.JMenuItem();
        addTestCaseGroup = new javax.swing.JMenuItem();
        addTestCase = new javax.swing.JMenuItem();
        addTestStep = new javax.swing.JMenuItem();
        addDataObject = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        delete = new javax.swing.JMenuItem();
        edit = new javax.swing.JMenuItem();
        clone = new javax.swing.JMenuItem();
        addPreConCase = new javax.swing.JMenuItem();
        addPostConCase = new javax.swing.JMenuItem();
        paste = new javax.swing.JMenuItem();
        oneToOneCopy = new javax.swing.JMenuItem();
        caseFilter = new javax.swing.JMenuItem();
        closeTree = new javax.swing.JMenuItem();
        copy = new javax.swing.JMenuItem();
        cut = new javax.swing.JMenuItem();
        addTestStepGroup = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        validateTree = new javax.swing.JMenuItem();
        searchParam = new javax.swing.JMenuItem();
        jSearchSeparator = new javax.swing.JSeparator();
        addTestPlan.setText("Add Test Plan");
        addTestPlan.setVisible(false);
        addTestPlan.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                newTestPlanAction(evt);
            }
        });
        jPopupMenu1.add(addTestPlan);

        addTestCaseGroup.setText("Add Test Case Group");
        addTestCaseGroup.setVisible(false);
        addTestCaseGroup.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                newTestCaseGroupAction(evt);
            }
        });
        jPopupMenu1.add(addTestCaseGroup);

        addTestCase.setText("Add Test Case");
        addTestCase.setVisible(false);
        addTestCase.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                newTestCaseAction(evt);
            }
        });
        jPopupMenu1.add(addTestCase);

        addPreConCase.setText("Add PreCondition Case");
        addPreConCase.setVisible(false);
        addPreConCase.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                newPreConTestCaseAction(evt);
            }
        });
        jPopupMenu1.add(addPreConCase);

        addPostConCase.setText("Add PostCondition Case");
        addPostConCase.setVisible(false);
        addPostConCase.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                newPostConTestCaseAction(evt);
            }
        });
        jPopupMenu1.add(addPostConCase);

        addTestStepGroup.setText("Add Test Step Group");
        addTestStepGroup.setVisible(false);
        addTestStepGroup.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                newTestStepGroupAction(evt);
            }
        });
        jPopupMenu1.add(addTestStepGroup);

        addTestStep.setText("Add Test Step");
        addTestStep.setVisible(false);
        addTestStep.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                newTestStepAction(evt);
            }
        });
        jPopupMenu1.add(addTestStep);

        addDataObject.setText("Add Data Object");
        addDataObject.setVisible(false);
        addDataObject.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                newDataObjectAction(evt);
            }
        });
        jPopupMenu1.add(addDataObject);
        jPopupMenu1.add(jSeparator1);

        delete.setText("Delete");
        delete.setVisible(false);
        delete.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                deleteNodeAction(evt);
            }
        });
        jPopupMenu1.add(delete);

        edit.setText("Edit");
        edit.setVisible(false);
        edit.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                editNodeAction(evt);
            }
        });
        jPopupMenu1.add(edit);

        oneToOneCopy.setText("1-to-1 Copy");
        oneToOneCopy.setVisible(false);
        oneToOneCopy.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                oneToOneCopyAction(evt);
            }
        });
        jPopupMenu1.add(oneToOneCopy);

        copy.setText("Copy");
        copy.setVisible(false);
        copy.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                copyAction(evt);
            }
        });
        jPopupMenu1.add(copy);

        cut.setText("Cut");
        cut.setVisible(false);
        cut.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                cutActionPerformed(evt);
            }
        });
        jPopupMenu1.add(cut);

        clone.setText("Clone");
        clone.setVisible(false);
        clone.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                cloneAction(evt);
            }
        });
        jPopupMenu1.add(clone);

        caseFilter.setText("Case Filter");
        caseFilter.setVisible(false);
        caseFilter.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                refreshTreeAction(evt);
            }
        });
        jPopupMenu1.add(caseFilter);


        paste.setText("Paste");
        paste.setVisible(false);
        paste.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                pasteAction(evt);
            }
        });
        jPopupMenu1.add(paste);

        validateTree.setText("Validate Node Ctrl+B");
        validateTree.setVisible(false);
        validateTree.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                validateTree(evt);
            }
        });
        jPopupMenu1.add(jSeparator3);
        jPopupMenu1.add(validateTree);
        searchParam.setText("Search Ctrl+F");
        searchParam.setVisible(false);
        searchParam.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                searchParameter(evt);
            }
        });
       
        jPopupMenu1.add(searchParam);
        jPopupMenu1.add(jSearchSeparator);

        closeTree.setText("Close Tree");
        closeTree.setVisible(false);
        closeTree.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(
                    java.awt.event.ActionEvent evt) {
                closeTree(evt);
            }
        });
        jPopupMenu1.add(closeTree);



    }// </editor-fold>

    private void validateTree(ActionEvent evt) {
        TestDesignControllerList.newInstance().getTreeController().operationChangeLastSearch("");
        TestDesignControllerList.newInstance().getTreeController().operationValidateTree();
    }

    private void searchParameter(ActionEvent evt) {
        String result = javax.swing.JOptionPane.showInputDialog("Enter Value:");
        if (result == null) {
            return;
        }
        if (!result.equals("")) {
            TestDesignControllerList.newInstance().getTreeController().operationChangeLastSearch(result);
            TestDesignControllerList.newInstance().getTreeController().operationExpandToNextNode();
        }
    }

    

    private boolean isCopyingSelected() {
        JRadioButton rbSelected = new JRadioButton("Selected Objects");
        JRadioButton rbAll = new JRadioButton("All Objects");
        rbAll.setSelected(true);
        ButtonGroup group = new ButtonGroup();
        group.add(rbSelected);
        group.add(rbAll);

        JPanel panel = new JPanel();
        JPanel panel2 = new JPanel();
        panel2.add(rbSelected);
        panel2.add(rbAll);
        panel.add(panel2);
        JOptionPane.showMessageDialog(null, panel);

        return rbSelected.isSelected();
    }

    private void newTestPlanAction(java.awt.event.ActionEvent evt) {

        TestDesignControllerList.newInstance().getTestPlanController().operationNewTestPlan();
    }

    private void newTestCaseGroupAction(java.awt.event.ActionEvent evt) {

        TestDesignControllerList.newInstance().getTestCaseGroupController().operationNewTestCaseGroup();
    }

    private void newTestCaseAction(java.awt.event.ActionEvent evt) {

        TestDesignControllerList.newInstance().getTestCaseController().operationNewTestCase();
    }

    private void newPreConTestCaseAction(java.awt.event.ActionEvent evt) {

        TestDesignControllerList.newInstance().getPreConditionCaseController().operationNewPreConditionCase();
    }

    private void newPostConTestCaseAction(java.awt.event.ActionEvent evt) {

        TestDesignControllerList.newInstance().getPostConditionCaseController().operationNewPostConditionCase();
    }

    private void newTestStepGroupAction(java.awt.event.ActionEvent evt) {

        TestDesignControllerList.newInstance().getTestStepGroupController().operationNewTestStepGroup();
    }

    private void newTestStepAction(java.awt.event.ActionEvent evt) {

        TestDesignControllerList.newInstance().getTestStepController().operationNewTestStep();
    }

    private void newDataObjectAction(java.awt.event.ActionEvent evt) {

        DesignPart part = TestDesignControllerList.newInstance().getTreeController().getSelectedTreeNode().getObject();
        String moduleTypeName = TestDesign.getModuleTypeName(((TestStepRefined) part).getReference().
                getModulTypeId());
        TestDesignControllerList.newInstance().getDataObjectController().operationNewDataObject(moduleTypeName);
    }

    private void deleteNodeAction(java.awt.event.ActionEvent evt) {

        int result =
                javax.swing.JOptionPane.showConfirmDialog(null,
                "Are you sure you want to delete this node?",
                "Confirm",
                javax.swing.JOptionPane.YES_NO_OPTION,
                javax.swing.JOptionPane.QUESTION_MESSAGE);
        if (result == javax.swing.JOptionPane.YES_OPTION) {
            String warningTxt = TestDesignControllerList.newInstance().
                    getTestStepController().
                    checkConstraintBeforeDelete();
            if (!warningTxt.equals("")) {
                JOptionPane.showMessageDialog(null,
                        warningTxt,
                        "Warning",
                        JOptionPane.WARNING_MESSAGE);
            }
            TestDesignControllerList.newInstance().getTreeController().operationDeleteTreeNode(TestDesignControllerList.newInstance().getTreeController().getSelectedMutableTreeNode());
        }
    }

    private void editNodeAction(java.awt.event.ActionEvent evt) {
        TestDesignTreeNode node = TestDesignControllerList.newInstance().getTreeController().getSelectedTreeNode();
        TestDesignControllerList.newInstance().getTestSuiteController().operationEditTestSuite(node);
        TestDesignControllerList.newInstance().getTestPlanController().operationEditTestPlan(node);
        TestDesignControllerList.newInstance().getTestCaseGroupController().operationEditTestCaseGroup(node);
        TestDesignControllerList.newInstance().getTestCaseController().operationEditTestCase(node);
        TestDesignControllerList.newInstance().getPreConditionCaseController().operationEditPreConditionCase(node);
        TestDesignControllerList.newInstance().getPostConditionCaseController().operationEditPostConditionCase(node);
        TestDesignControllerList.newInstance().getTestStepController().operationEditTestStep(node);
        TestDesignControllerList.newInstance().getDataObjectController().operationEditDataObject(node);
        TestDesignControllerList.newInstance().getTestStepGroupController().operationEditTestStepGroup(node);
    }

    private void oneToOneCopyAction(java.awt.event.ActionEvent evt) {
        processSelectedOnly = isCopyingSelected();
        TestDesignControllerList.newInstance().getPopupController().operationSetObject(TestDesignControllerList.newInstance().getTreeController().getSelectedMutableTreeNode(),
                "Paste (1-to-1 Copy)");
    }

    private void cutActionPerformed(ActionEvent evt) {
        TestDesignControllerList.newInstance().getPopupController().operationSetObject(TestDesignControllerList.newInstance().getTreeController().getSelectedMutableTreeNode(),
                "Paste (Cut)");
    }

    private void refreshTreeAction(java.awt.event.ActionEvent evt) {
        TestDesignControllerList.newInstance().getSearchController().startTestDesignSelecter(TestDesignControllerList.newInstance().getTreeController().getSelectedMutableTreeNode());
        TestDesignControllerList.newInstance().getTreeController().operationRefreshTree();
    }

    private void cloneAction(java.awt.event.ActionEvent evt) {
        processSelectedOnly = isCopyingSelected();
        TestDesignControllerList.newInstance().getPopupController().operationSetObject(TestDesignControllerList.newInstance().getTreeController().getSelectedMutableTreeNode(),
                "Paste (Clone)");
    }

    private void pasteAction(java.awt.event.ActionEvent evt) {
        if (paste.getText().equals("Paste (Copy)")) {
            try {
                TestDesignControllerList.newInstance().getTreeController().operationPasteCopy(TestDesignControllerList.newInstance().getPopupController().operationGetObject(), TestDesignControllerList.newInstance().getTreeController().getSelectedMutableTreeNode(), false, processSelectedOnly);
            } catch (PersistentException ex) {
                ex.printStackTrace();
            }
        } else if (paste.getText().equals("Paste (Clone)")) {
            TestDesignControllerList.newInstance().getTreeController().operationPasteClone(TestDesignControllerList.newInstance().getPopupController().operationGetObject(), TestDesignControllerList.newInstance().getTreeController().getSelectedMutableTreeNode(), processSelectedOnly);
        } else if (paste.getText().equals("Paste (1-to-1 Copy)")) {
            try {
                TestDesignControllerList.newInstance().getTreeController().operationPasteCopy(TestDesignControllerList.newInstance().getPopupController().operationGetObject(), TestDesignControllerList.newInstance().getTreeController().getSelectedMutableTreeNode(), true, processSelectedOnly);
            } catch (PersistentException ex) {
                ex.printStackTrace();
            }
        } else if (paste.getText().equals("Paste (Copy Selection)")) {
            try {
                TestDesignControllerList.newInstance().getTreeController().operationPasteCopy(TestDesignControllerList.newInstance().getPopupController().operationGetObject(), TestDesignControllerList.newInstance().getTreeController().getSelectedMutableTreeNode(), false, false);
            } catch (PersistentException ex) {
                ex.printStackTrace();
            }
        } else if (paste.getText().equals("Paste (Clone Selection)")) {
            TestDesignControllerList.newInstance().getTreeController().operationPasteClone(TestDesignControllerList.newInstance().getPopupController().operationGetObject(), TestDesignControllerList.newInstance().getTreeController().getSelectedMutableTreeNode(), false);
        } else if (paste.getText().equals("Paste (1-to-1 Copy Selection)")) {
            try {
                TestDesignControllerList.newInstance().getTreeController().operationPasteCopy(TestDesignControllerList.newInstance().getPopupController().operationGetObject(), TestDesignControllerList.newInstance().getTreeController().getSelectedMutableTreeNode(), true, false);
            } catch (PersistentException ex) {
                ex.printStackTrace();
            }
        } else if (paste.getText().equals("Paste (Cut)")) {
            try {
                TestDesignControllerList.newInstance().getTreeController().operationPasteCut(TestDesignControllerList.newInstance().getPopupController().operationGetObject(), TestDesignControllerList.newInstance().getTreeController().getSelectedMutableTreeNode());
            } catch (PersistentException ex) {
                ex.printStackTrace();
            }
        }
        
    }

    private void closeTree(java.awt.event.ActionEvent evt) {
        TestDesignControllerList.newInstance().getTreeController().operationCloseTree();
    }

    private void copyAction(java.awt.event.ActionEvent evt) {
        processSelectedOnly = isCopyingSelected();
        TestDesignControllerList.newInstance().getPopupController().operationSetObject(TestDesignControllerList.newInstance().getTreeController().getSelectedMutableTreeNode(),
                "Paste (Copy)");
    }

    // <editor-fold defaultstate="collapsed" desc=" Model Event Handling Code ">
    /**
     * Called by the controller when it needs to pass along a property change
     * from a model.
     * @param evt The property change event
     */
    public void modelPropertyChange(PropertyChangeEvent evt) {
        boolean value = false;
        String event = evt.getNewValue().toString();
        if (event.equals("true")) {
            value = true;
        } else if (event.equals("false")) {
            value = false;
        } else {
        }
        if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_TEST_PLAN_VISIBILITY)) {
            addTestPlan.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_TEST_CASE_GROUP_VISIBILITY)) {
            addTestCaseGroup.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_TEST_CASE_VISIBILITY)) {
            addTestCase.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_PRE_CONDITION_CASE_VISIBILITY)) {
            addPreConCase.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_POST_CONDITION_CASE_VISIBILITY)) {
            addPostConCase.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_TEST_STEP_GROUP_VISIBILITY)) {
            addTestStepGroup.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_TEST_STEP_VISIBILITY)) {
            addTestStep.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_VALIDATE_TREE_VISIBILITY)) {
            validateTree.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_DATA_OBJECT_VISIBILITY)) {
            addDataObject.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_DELETE_VISIBILITY)) {
            delete.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_EDIT_VISIBILITY)) {
            edit.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_1TO1_COPY_VISIBILITY)) {
            oneToOneCopy.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_COPY_VISIBILITY)) {
            copy.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_CUT_VISIBILITY)) {
            cut.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_CLONE_VISIBILITY)) {
            clone.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_SEARCH_VISIBILITY)) {
            caseFilter.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_PASTE_CLONE_VISIBILITY)) {
            paste.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_CLOSE_TREE_VISIBILITY)) {
            closeTree.setVisible(value);
        } else if (evt.getPropertyName().equals(
                PopupController.SET_POPUP_MENU_SEARCH_PARAM_VISIBILITY)) {
            searchParam.setVisible(value);
        }else if (evt.getPropertyName().equals(PopupController.SET_PASTE_LABEL)) {
            paste.setText(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(PopupController.OPEN_POPUP_MENU)) {
            LinkedList coordinates = (LinkedList) evt.getNewValue();
            Component component = (Component) coordinates.get(0);
            Integer mouseX = (Integer) coordinates.get(1);
            Integer mouseY = (Integer) coordinates.get(2);
            jPopupMenu1.show(component,
                    mouseX,
                    mouseY);
        }
    }
    // </editor-fold>  
    // Variables declaration - do not modify
    private javax.swing.JMenuItem addTestPlan;
    private javax.swing.JMenuItem addTestCaseGroup;
    private javax.swing.JMenuItem addTestCase;
    private javax.swing.JMenuItem addTestStep;
    private javax.swing.JMenuItem addDataObject;
    private javax.swing.JMenuItem addPreConCase;
    private javax.swing.JMenuItem addPostConCase;
    private javax.swing.JMenuItem delete;
    private javax.swing.JMenuItem edit;
    private javax.swing.JMenuItem clone;
    private javax.swing.JMenuItem paste;
    private javax.swing.JMenuItem closeTree;
    private javax.swing.JMenuItem copy;
    private javax.swing.JMenuItem cut;
    private javax.swing.JMenuItem oneToOneCopy;
    private javax.swing.JMenuItem addTestStepGroup;
    private javax.swing.JMenuItem caseFilter;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JMenuItem validateTree;
    private javax.swing.JMenuItem searchParam;
    private javax.swing.JSeparator jSearchSeparator;
}
